#!/bin/bash
set -uxe
cpath=$(cd `dirname $0`;pwd)
source ../lib/lib.sh

function usage()
{
    log_info "src_prj des_prj co_flag old_brinfo new_brinfo"
    log_info "example: EulerOS:Test1 EulerOS:Test2 0/1 next centos_7.5"
    log_info "增量同步工程 EulerOS:Test1到EulerOS:Test2 1表示本地已经有src_prj，不用重复osc co,默认是0"
    log_info "src_prj des_prj参数必须传入，无分支信息则只拷贝不做_service文件修改"
}

function create_prj()
{
    prj=$1
    pushd $cpath/obs_space
    osc meta prj $prj > file1
    file2=${prj}_L/.osc/${prj}.meta
    if ! diff file1 $file2 &>/dev/null; then
        osc meta prj "$prj" --file="${prj}_L/.osc/${prj}.meta"
    fi
    osc meta prjconf $prj > file1
    file2=${prj}_L/.osc/${prj}.prjconf
    if ! diff file1 $file2 &>/dev/null; then
        osc meta prjconf "$prj" --file="${prj}_L/.osc/${prj}.prjconf"
    fi

    pushd ${prj}_L
    ls | sort > ../sour_dir.list
    popd
    osc ls $prj | sort > dest_dir.list
    set +e
    changelist=`diff -ur sour_dir.list dest_dir.list  | egrep "^[+-]" | grep -v '^---'| grep -v '^+++' | grep -v '\.osc'`
    set -e
    for line in $changelist
    do
        flag=${line:0:1}
        pkg=${line:1}
        rm -rf $prj
        if [ "$flag" == "+" ]; then
            osc co $prj $pkg
            pushd $prj
            osc rm $pkg
            osc ci $pkg -m 'update'
            popd
        else
            osc co $prj `osc ls $prj 2>/dev/null | sed -n '1p'`
            cp -a ${prj}_L/$pkg $prj
            rm -rf $prj/$pkg/.osc
            pushd $prj
            osc add $pkg
            osc ci $pkg -m 'update'
            popd
        fi
    done

    for pkg in `cat sour_dir.list`
    do
        echo > file1
        if osc ls $prj $pkg | grep _service &>/dev/null; then
            osc cat $prj $pkg _service > file1
        fi
        if [ -f ${prj}_L/${pkg}/_service ]; then
            file2=${prj}_L/${pkg}/_service
        else
            echo > file2
            file2=file2
        fi
        if ! diff file1 $file2 &>/dev/null; then
            rm -rf $prj
            osc co $prj $pkg
            cp ${prj}_L/${pkg}/_service $prj/$pkg/
            pushd  $prj/$pkg
            files=`osc status|grep ^?|awk '{print $2}'`
            if [ -n "$files" ]; then
                osc add $files
            fi
            osc ci -m 'ok'
            popd
        fi

        osc meta pkg $prj $pkg > file1
        file2=${prj}_L/$pkg/.osc/_meta
        if ! diff file1 $file2 &>/dev/null; then
            osc meta pkg "$prj" "$pkg" --file="${prj}_L/${pkg}/.osc/_meta"
        fi
    done
}


function osc_env_test()
{
    expect -c "
        set timeout -1
        spawn osc api /about
        expect {
            \"?Enter*?\" {
                send \"2\r\"
                exp_continue
            }
                eof {
                    catch wait result
                    exit [lindex \$result 3]
                }
            }
            expect {
                eof {
                    catch wait result
                    exit [lindex \$result 3]

                }
    }
    "
    if [ $? -ne 0 ]; then
        log_error "osc environment test failed"
    fi
}

function src_prj_download()
{
    src_prj=$1
    des_prj=$2

    osc_env_test

    rm -rf $cpath/obs_space
    mkdir  $cpath/obs_space
    pushd  $cpath/obs_space
    osc co $src_prj
    osc meta prj "$src_prj" > ${src_prj}/.osc/${src_prj}.meta
    osc meta prjconf "$src_prj" > ${src_prj}/.osc/${src_prj}.prjconf
    mv ${src_prj} ${des_prj}_L
    mv ${des_prj}_L/.osc/${src_prj}.meta ${des_prj}_L/.osc/${des_prj}.meta
    mv ${des_prj}_L/.osc/${src_prj}.prjconf ${des_prj}_L/.osc/${des_prj}.prjconf
    find ${des_prj}_L -type f | xargs sed -i "s/$src_prj/$des_prj/g"
    find ${des_prj}_L -type f | xargs sed -i '/<person userid/d'
    find ${des_prj}_L -type f | xargs sed -i '/<group groupid/d'
    if [ -n "$old_brinfo" -a -n "$new_brinfo" ]; then
        find ${des_prj}_L -type f | xargs sed -i "s/$old_brinfo/$new_brinfo/g"
    fi
    popd
}

usage
if [ -z "$src_prj" -o -z "$des_prj" ]; then
    exit 0
else
   if [ $co_flag -eq 0 ]; then
       src_prj_download $src_prj $des_prj
   fi
   create_prj $des_prj
fi
